package com.example.performance.r2dbc.entity.performance;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author zyjia
 * @since 09/05/2023 11:00 AM
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ResultSetUtil {


    public static Double getDouble(ResultSet resultSet, String columnName) throws SQLException {
        double result = resultSet.getDouble(columnName);
        if (resultSet.wasNull()) {
            return Double.NaN;
        }
        return result;
    }

    public static Double getDouble(ResultSet resultSet, int columnIndex) throws SQLException {
        double result = resultSet.getDouble(columnIndex);
        if (resultSet.wasNull()) {
            return Double.NaN;
        }
        return result;
    }

    public static Long getLong(ResultSet resultSet, String columnName) throws SQLException {
        long result = resultSet.getLong(columnName);
        if (resultSet.wasNull()) {
            return null;
        }
        return result;
    }

    public static Long getLong(ResultSet resultSet, int columnIndex) throws SQLException {
        long result = resultSet.getLong(columnIndex);
        if (resultSet.wasNull()) {
            return null;
        }
        return result;
    }

    public static Integer getInteger(ResultSet resultSet, String columnName) throws SQLException {
        int result = resultSet.getInt(columnName);
        if (resultSet.wasNull()) {
            return null;
        }
        return result;
    }

    public static Integer getInteger(ResultSet resultSet, int columnIndex) throws SQLException {
        int result = resultSet.getInt(columnIndex);
        if (resultSet.wasNull()) {
            return null;
        }
        return result;
    }
}